<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		changeConfigAttr,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	import moment from'@/static/js/moment.js'
	export default {
		data() {
			return buildFormData({
				userCache:"cmf-report-tmeetingReportItem-user-cache",
				frequencyCache:'cmf-report-tmeetingReportItem-frequency-cache'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		methods: {
			verifyData(){
				let _that = this
				if(_that.subEditorObj["title2"].workList.length > 15){
					_that.om.toast('会议报告事项清单条数最多只能编辑15条')
					return false
				}else{
					return true
				}
			},
			onSelectPlanDate(date){
				let _that = this
				if(!moment(date).isSameOrAfter(moment().format('YYYY-MM-DD'))){
					_that.om.toast('请选择当前日期或之后的时间')
					_that.$refs.autoform.clear("title1","planDate")
				}
			},
			onSelectPos(item){
				let _that  = this
				_that.$refs.autoform.formdata.instName = item.instName
			},
			async getPosList(id){
				let _that = this
				await _that.getPosComboboxByUser(id)
			},
			getPosComboboxByUser(id){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/getPosComboboxByUser",
					{
						userId:id
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title1","posId","data",configSelectData(res,"id","posName"))
					if(_that.$refs.autoform.formdata.posId){
						_that.$refs.autoform.initSelectIndex("title1","posId")
					}else{
						_that.$refs.autoform.initSelectVal("title1","posId")
					}
				})
			},
			initLoad(options) {
				let _that = this;
				var fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url:"tsysdict/getListByCode",
						param:{
							code:'36285'
						}
					}
				]
				if(fileId){
					ajaxArr.push({
						url:"tmeetingReportItem/getInfo",
						param:{id:fileId},
						method:'post'
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posName"),mainData)
					changeEditor("title1","meetingCateId","data",configSelectData(res[1],"id","dicname"),mainData)
					changeEditor("title1","userId",'isSearchUser',true,mainData)
					changeEditor("title1","userId",{
						"link":'../../public/searchUser/searchUser',
						"linkParam": {
							cachename: _that.userCache,
							isUserPos:true,
							isSingle:true
							
						}
					},null,mainData)
					changeEditor("title1","frequencyId",{
						"link":'../../public/search/search',
						"linkParam": {
							options:JSON.stringify({
								cateId:''
							}),
							url:'tmeetingReportItem/getCateTree',
							cachename: _that.frequencyCache,
							initload:true,
							namefield:'text'
						}
					},null,mainData)
					if(_that.subEditorObj["title2"]){
						let att = {
						}
						let configData = _that.subConfigData["title2"]
						if(hasEditor("title2","itemName",configData)){
							let title = getEditorTitle("title2","itemName",configData)
							att.titleTemplate = title+":<%= itemName %>"
						}
						changeEditor("title2","dataStr",att,mainData)
						
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[2].value.tmeetingReportItem){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[2].value.tmeetingReportItem[index])
								_that.$set(_that.formdata,index,res[2].value.tmeetingReportItem[index])
							}
						}
						var dataList = _that._.cloneDeep(res[2].value.dataList)
						_that.subEditorObj["title2"].workList = dataList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "dataStr", JSON.stringify(dataList))
						_that.$set(_that.formdata, "dataStr", JSON.stringify(dataList))
						delete res[2].value.dataList
						if(_that.$refs.autoform.formdata.posId){
							_that.getPosList(_that.$refs.autoform.formdata.userId)
						}
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.userId){
							_that.$refs.autoform.formdata.userId = _that.userId
							_that.$refs.autoform.formdata.userName = _that.userName
							_that.getPosList(_that.$refs.autoform.formdata.userId)
						}
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						}
						_that.$refs.autoform.initSelectIndex("title1","meetingCateId")
					})
				}, options)
			},
			formSubmit() {
				let _that = this
				_that.om.formSave("tmeetingReportItem/addTmeetingReportItem",_that.formdata,_that)

			}
			
		},
		onUnload(){
		//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
		//#endif
		},
		onLoad(options) {
			this.initValue.userinfoId = this.userId
			this.initLoad(options)
		},
		onShow() {
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.userCache,
					success:(res)=>{
						if(res.data){
							let d = res.data[0]
							let formdata = _that.$refs.autoform.formdata
							formdata.userId = d.id
							formdata.userName = d.username
							formdata.posId = d.posId
							formdata.posName = d.posName
							formdata.instName = d.instName
							_that.getPosComboboxByUser(d.id)
							uni.removeStorage({
								key:_that.userCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.frequencyCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.frequencyId = d.id
							formdata.frequencyName = d.text
							uni.removeStorage({
								key:_that.frequencyCache
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
       